package com.wzp.yyptproject.controller;

import com.wzp.yyptproject.entity.ImgName;
import com.wzp.yyptproject.entity.User;
import com.wzp.yyptproject.repository.UserRepository;
import com.wzp.yyptproject.service.UserService;
import com.wzp.yyptproject.until.FtpFileUtil;
import com.wzp.yyptproject.until.FtpOperation;
import com.wzp.yyptproject.until.ImgUtil;
import com.wzp.yyptproject.until.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.*;
import java.sql.Wrapper;

@Slf4j
@RestController
@CrossOrigin
public class imgController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRepository userRepository;
    private ImgName imgName = new ImgName();
@Autowired
private FtpOperation ftpOperation;
    @Autowired
    private HttpServletRequest request;
@Autowired
private FtpFileUtil ftpFileUtil;
    @RequestMapping(value = "/imgUpload")
    public String imgUpload(HttpServletRequest req, MultipartHttpServletRequest multiReq)
            throws ServletException, IOException {
//        System.out.println("-----D:\img1");//我这里用的springboot 在application.properties中配置，使用@Value 获取的文件上传目录

        MultipartFile file = multiReq.getFile("file");
        log.info("路径"+file);
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.indexOf("."));
        String localFileName = MD5Util.md5(file.getInputStream()) + suffix;


        File localFile = new File("D:\\yyptproject\\yyptproject\\src\\main\\web\\img"+localFileName);

        Part inputfile = req.getPart("file");
        String path = req.getServletContext().getRealPath("") + "img";
        System.out.println("服务器路径"+path);
        ImgUtil.imgController(inputfile, path, localFileName);
        //本地存储
        String localpath = "D:\\yyptproject\\yyptproject\\src\\main\\web\\img";
        ImgUtil.imgController(inputfile, localpath, localFileName);
        InputStream is = inputfile.getInputStream();
        imgName.setImgname(localFileName);
//        try {
//            ftpOperation.uploadToFtp(is,localFileName,false);
//            log.info("上传成功");
//        }catch (Exception e){
//            log.info("上传失败");
//        }


        try {
            ftpFileUtil.uploadFile(localFileName,is);
            log.info("上传成功");
        }catch (Exception e){
            log.info("上传失败");
        }


//        if (!localFile.exists()) {
//            localFile.createNewFile();
//            FileOutputStream fos = new FileOutputStream(
//                    localFile);
//            FileInputStream fs = (FileInputStream) multiReq.getFile("img").getInputStream();
//            byte[] buffer = new byte[1024];
//            int len = 0;
//            while ((len = fs.read(buffer)) != -1) {
//                fos.write(buffer, 0, len);
//            }

//
//            fos.close();
//            fs.close();

//        } else {
//
//
//            log.info("文件已存在！！");
//            request.getServletContext().getRealPath("/");
//            log.info(""+request.getServletContext().getRealPath("/"));
//            log.info("测试111"+"C:Users\\22761\\AppData\\Local\\Temp\\tomcat-docbase.65592573201502376.8001\\" + localFileName);
//            log.info("测试222"+request.getServletContext().getRealPath("/") + localFileName);
//
//            log.info("测试333"+imgName.getImgname());
//
//
//        }

        return  localFileName;

//        return WrapMapper.wrap(
//                Wrapper.SUCCESS_CODE,
//                Wrapper.SUCCESS_MESSAGE,
//                "http://localhost:8080/img/" + localFileName);//这里是我执行封装的返回结果，也可以使用map,
    }
    @PostMapping("/updateimg")
    public String findAndByUnameAndUidCard(@RequestBody User user) {
        log.info("测试444"+imgName.getImgname());
        User user1= userRepository.findAndByUnameAndUidcard(user.getUname(),user.getUidcard());
        log.info(user1.getUname());
        log.info(user1.getUidcard());
        if (user1!=null){
          user1.setUUid(userRepository.findAndByUUid(user1.getUUid()).getUUid());
          user1.setUhead(imgName.getImgname());
          userRepository.save(user1);
          return "success";
        }
        return "failed";
    }

}
